home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / mcu / dtmf.arc / DTMF.TXT next >
Text File  |  1989-12-17  |  14KB  |  331 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                     When  designing products for  the consumer market, cost
  8.           is  one of the most critical design criteria.  Usually one of the
  9.           best ways to reduce product cost is to move as much functionality
  10.           as   possible into the product's firmware, reducing the unit cost
  11.           of the hardware.
  12.                      
  13.                     In  a telephone  design,  generating Dual Tone Multiple
  14.           Frequency  (DTMF)  signalling  is  usually  done  by  specialized
  15.           hardware  dialler chips.   However,  these devices  typically add
  16.           more  than a  dollar to the  manufacturing cost of  the phone; it
  17.           would  be  desirable to  be  able to  do  this in  software, with
  18.           minimal hardware support.
  19.                      
  20.                     Another  benefit  of a  software implementation  is the
  21.           added  flexibility of  generating tones  other than  the standard
  22.           DTMF  tone pairs.   This might  be useful  for special signalling
  23.           with  propietary  equipment,  or  other  tones  such  as  audible
  24.           feedback    when  the  user  presses   keys,  etc.    Also, there
  25.           applications  other  than  telephony  which  could  benefit  from
  26.           software sinewave generation.
  27.                      
  28.                     This    report examines    the requirements    for DTMF
  29.           dialling, and shows how these requirements may be met at very low
  30.           cost  with  a software  implementation  for both  the  68HC05 and
  31.           68HC11 family of microcontrollers.
  32.                      
  33.                                        DTMF Requirements
  34.                      
  35.                     DTMF    signalling  represents  16  binary  digit codes
  36.           through  the use of  sine wave tone pairs,  organized into a high
  37.           group  (1200-1700 Hz)  and a low  group (600-1000  Hz). There are
  38.           four tones in each group.
  39.                      
  40.                     As  shown  below,  the  tones  are  associated  with  a
  41.           particular  row or column on the telephone keypad, which is shown
  42.           below with the associated frequencies. Column four is defined but
  43.           is  not  usually  implemented  on  a  telephone.    Signalling is
  44.           accomplished  by  transmitting one  tone  from each  group  for a
  45.           minimum of 50 ms, followed by a silent period of at least 50 ms.
  46.                      
  47.                               Column 1  Column 2  Column 3  Column 4
  48.                               1209 Hz   1336 Hz   1477 Hz   1633 Hz
  49.                      
  50.                     Row 1          "1"       "2"       "3"       "A"
  51.                     697 Hz
  52.                      
  53.                     Row 1          "4"       "5"       "6"       "B"
  54.                     770 Hz
  55.                      
  56.                     Row 1          "7"       "8"       "9"       "C"
  57.                     852 Hz
  58.                      
  59.                     Row 1          "*"       "0"       "#"       "D"
  60.                     941 Hz
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.                      
  74.                     Generating  network-quality DTMF is not a trivial task.
  75.           Industry  specifications generally require frequency errors to be
  76.           less than 1%, and total harmonic distortion (THD) to be less than
  77.           10%.   Additionally, the frequency response of the telephone line
  78.           will  generally roll off at  high frequencies, requiring the high
  79.           group  of tones to be transmitted  at a higher amplitude than the
  80.           low  group.   The telephony  buzzword for  this characteristic is
  81.           twist.
  82.                      
  83.                                        Software Approach
  84.                      
  85.                     The basic technique to generate these tones in software
  86.           involves  a table lookup of two sine waves, adding them together,
  87.           and  outputting them through a D/A  converter. In order to reduce
  88.           harmonic  content to  a minimum, they  must be sampled  at a high
  89.           enough  rate  so  that  the  noise  introduced  at  the  sampling
  90.           frequency  can  be easily  filtered from  the  output.   A sample
  91.           period  of 128  us was  chosen for  this example;   this produces
  92.           sampling noise at 7.8 Khz.
  93.                      
  94.                     In  order to save memory, a single 256 sample sine wave
  95.           table  is used. If we  just picked out one  sample per period and
  96.           output that data to the D/A, the output frequency would be
  97.                      
  98.                                         1/(256*128 us)
  99.                      
  100.                     or  30.5  Hz.    However, we  need  the  flexibility to
  101.           generate  many different frequencies, none  of which are 30.5 Hz.
  102.           To  do this,  we can  skip several  samples after  one is output,
  103.           wrapping  around to the start  of the table when  we skip off the
  104.           end.   This has the effect of multiplying the output frequency by
  105.           the  number of samples  skipped. For example,  if we skipped past
  106.           two  samples to arrive at the next  one to be output, we would be
  107.           outputting  only every third sample.   The output frequency would
  108.           be three times the base frequency or
  109.                      
  110.                                  3/(256 * 128 us) = 91.55 Hz.
  111.                      
  112.                     So the lowest frequency we can generate is 30.5 Hz, and
  113.           the highest is
  114.                      
  115.                                  128/(256 * 128 us) = 3.9 Khz,
  116.                      
  117.                     Since  we cannot go above  1/2 the sample frequency due
  118.           to  Nyquist's sampling theory.   Our frequency resolution is 30.5
  119.           Hz.
  120.                      
  121.                     So  in order to generate DTMF, we must use two pointers
  122.           stepping  through this 256  byte table in  memory; at each sample
  123.           period,  two  values are  retrieved from  the table,  summed, and
  124.           output  to a DAC. The pointers are then updated by adding a value
  125.           to  each to  step through  the table.   This value  is called the
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.           pointer  interval, and  is different  for each  frequency that we
  140.           must generate.
  141.                      
  142.                                Calculating the Pointer Intervals
  143.                      
  144.                      
  145.                     The general form of our equation is
  146.                      
  147.                     Fout      = Interval/(Table Size * Sample Period)
  148.                      
  149.                      
  150.                     So  the  interval  is calculated  by  flipping  that to
  151.           become
  152.                      
  153.                     Interval  = Table Size * Sample Period * Fout
  154.                      
  155.                      
  156.                     Let's  run through an example.  The frequency for row 1
  157.           on  the DTMF keypad is 697 Hz. If we use a sine wave table of 256
  158.           entries,
  159.                      
  160.                     Interval  = 256 * 128 us * 697 Hz
  161.                               = 22.83
  162.                      
  163.                     We    can't  step   through  the   table  by fractional
  164.           intervals,  so we  round the  interval to  23.   If we  plug that
  165.           number back into the frequency equation, our actual Fout is:
  166.                      
  167.                      
  168.                     Fout      = 23/(256 * 128 us)
  169.                               = 701.9 Hz.
  170.                      
  171.                     This  gives  us a  frequency  error of  0.70%  which is
  172.           acceptable.
  173.                      
  174.                     Below  is  a  table  of  DTMF  frequencies  which  were
  175.           calculated  using the above equations. The actual frequencies are
  176.           also  shown with the percentage error  calculated.  A Lotus 1-2-3
  177.           compatible   spreadsheet  file,  DTMF.WKS,   is  available  which
  178.           calculates  these intervals  based on  the sine  wave table size,
  179.           sample   period,  and  desired  frequency,   as  well  as  actual
  180.           frequencies and error.
  181.                      
  182.                      
  183.                                      Software DTMF Dialler
  184.                                     Using Fixed Sample Rate
  185.                      
  186.                     Sample Period (uS)  128
  187.                     Sample Table Size   256
  188.                      
  189.                     Frequency ->   697       770       852       941
  190.                     Interval       23        25        28        31
  191.                     Actual         701.90    762.94    854.49    946.04
  192.                     Error (%)      0.70      -0.92          0.29      0.54
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.                      
  206.                     Frequency ->   1209      1336      1477      1633
  207.                     Interval       40        44        48        54
  208.                     Actual         1220.70   1342.77   1464.84   1647.95
  209.                     Error (%)      0.97      0.51      -0.82          0.92
  210.                      
  211.                     Implementing High Tone Pre Emphasis
  212.                      
  213.                     In  order to compensate  for the rolloff characteristic
  214.           of  most telephone lines, the high group of tones must be about 1
  215.           to  3 db  higher power  than the  low group.   This  equates to a
  216.           voltage multiplication of about 1.12 to 1.41.  A convenient value
  217.           might  be 1.25  (1 and 1/4th),  since 1/4th is  a binary fraction
  218.           which can be achieved by shifting instead of a full multiply.
  219.                      
  220.                     To  implement this, the sine  table contains values for
  221.           the  low frequency row tones.  When the high group sample is read
  222.           from  the sine table, it  is shifted right 2  bits (divide by 4),
  223.           and then the same sine value is added again into the accumulator,
  224.           producing the 1.25 multiplication.  The pre emphasis is therefore
  225.                      
  226.                     db   = 20 log (V1/V2)
  227.                          = 20 log (1.25)
  228.                          = 1.94 db.
  229.                      
  230.                      
  231.                     Calculating Sine Values to Avoid Overflow
  232.                      
  233.                     The  values in the  sine wave table  must calculated to
  234.           avoid  overflow errors when the two samples are summed and output
  235.           to  the DAC.  In this example, an 8 bit D/A converter is used, so
  236.           our  maximum data values  are +127 to  -128.  If  two samples are
  237.           retrieved  from  the table,  added, and  output  to the  DAC, the
  238.           values  in  the  table  must  be between  +63  and  -64  to avoid
  239.           overflow.   In practice, they will  be somewhat smaller since the
  240.           column tones are pre emphasized.
  241.                      
  242.                     The DAC output value is calculated by
  243.                      
  244.                     MaxOutput = MaxSineValue * (1 + PreEmphasis)
  245.                      
  246.                     To solve for the MaxSineValue, rearrange the equation:
  247.                      
  248.                     MaxSineValue   = MaxOutput / (1 + PreEmphasis)
  249.                                    = 127 / (1 + 1.25)
  250.                                    = 56.4
  251.                      
  252.                     So  the sine values in the  table must vary between +/-
  253.           56.
  254.                      
  255.                     A  C program called MAKESINE.C  is available which will
  256.           output  a sine table based on user-defined table size and maximum
  257.           output  value.  The  output format is  compatible with most cross
  258.           assemblers for Motorola microcontrollers.
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.                      
  272.                     Benchmarking the Assembly Language Code
  273.                      
  274.                     Two  programs are included,  DTMF05.ASM and DTMF11.ASM,
  275.           which  implement the algorithm for  68HC05 and 68HC11 processors.
  276.           Both  programs use the  timer to generate an  interrupt at 128 us
  277.           intervals;   at each  interrupt, the program  calculates the next
  278.           value  to be output, and then updates the row and column pointers
  279.           based on pre-calculated pointer intervals.
  280.                      
  281.                     Looking  at the code, we see  that the 68HC05 takes 116
  282.           clocks  and 464 bytes of code to generate DTMF;  the 68HC11 takes
  283.           119  clocks and 457  bytes of code.   The additional clock cycles
  284.           for  the HC11 is  caused by the  requirement to always manipulate
  285.           16-bit  addresses, whereas the  HC05 can get  away with byte-size
  286.           address  calculations  applied  to  16-bit  offsets.    The HC05,
  287.           operating  at 2 MHz bus speed, uses 116/256 clock cycles (45%) to
  288.           service the DTMF interrupt.  The HC11 is slightly higher at 46%.
  289.                      
  290.                     THD Performance
  291.                      
  292.                     Harmonic  Distortion performance has  not been measured
  293.           at  this time.   I  feel that  a simple  two-pole filter  will be
  294.           sufficient  to reduce  harmonic content to  the point  that it is
  295.           below the 10% limit for network operation.
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.